<template>
  <div class="mobile-bottom-nav mobile-only">
    <router-link to="/" class="mobile-bottom-nav-item" :class="{ active: isActive('Home') }">
      <i class="el-icon-s-home mobile-bottom-nav-icon"></i>
      <span>首页</span>
    </router-link>
    <router-link to="/idea" class="mobile-bottom-nav-item" :class="{ active: isActive('Idea') }">
      <i class="el-icon-light-rain mobile-bottom-nav-icon"></i>
      <span>创意</span>
    </router-link>
    <router-link to="/job" class="mobile-bottom-nav-item" :class="{ active: isActive('job') }">
      <i class="el-icon-menu mobile-bottom-nav-icon"></i>
      <span>任务</span>
    </router-link>
    <router-link to="/notifications" class="mobile-bottom-nav-item" :class="{ active: isActive('notifications') }" @click.native="handleNotificationClick">
      <div class="notification-icon-container">
        <i class="el-icon-bell mobile-bottom-nav-icon"></i>
        <span v-if="unreadCount > 0" class="notification-badge">{{ unreadCount > 99 ? '99+' : unreadCount }}</span>
      </div>
      <span>通知</span>
    </router-link>
    <router-link to="/user" class="mobile-bottom-nav-item" :class="{ active: isActive('user_set') }">
      <i class="el-icon-user mobile-bottom-nav-icon"></i>
      <span>我的</span>
    </router-link>
  </div>
</template>

<script>
import { getUnreadNotificationsCount, markAllNotificationsAsRead } from '@/api/api';

export default {
  name: 'MobileNav',
  data() {
    return {
      unreadCount: 0,
      pollingInterval: null
    };
  },
  methods: {
    isActive(routeName) {
      return this.$route.name === routeName;
    },
    async fetchUnreadCount() {
      try {
        const response = await getUnreadNotificationsCount();
        console.log('移动端获取未读通知数量响应:', response);
        // 检查不同的返回格式
        if (response.hasOwnProperty('count')) {
          this.unreadCount = response.count || 0;
        } else if (typeof response === 'number') {
          this.unreadCount = response;
        } else {
          this.unreadCount = 0;
        }
      } catch (error) {
        console.error('获取未读通知数量失败:', error);
        this.unreadCount = 0;
      }
    },
    // 处理通知图标点击事件
    async handleNotificationClick() {
      if (this.unreadCount > 0) {
        try {
          await markAllNotificationsAsRead();
          // 更新未读通知计数
          this.unreadCount = 0;
          console.log('已全部标记为已读');
        } catch (error) {
          console.error('标记已读失败:', error);
        }
      }
    }
  },
  mounted() {
    // 初始化时获取一次未读数量
    this.fetchUnreadCount();
    
    // 定期轮询获取最新未读通知数量
    this.pollingInterval = setInterval(() => {
      this.fetchUnreadCount();
    }, 30000); // 30秒轮询一次
  },
  beforeDestroy() {
    // 清除轮询定时器
    if (this.pollingInterval) {
      clearInterval(this.pollingInterval);
    }
  }
}
</script>

<style scoped>
.mobile-bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #fff;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  z-index: 1000;
}

.mobile-bottom-nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #666;
  text-decoration: none;
  padding: 4px 0;
}

.mobile-bottom-nav-item.active {
  color: #FFBD00;
}

.mobile-bottom-nav-icon {
  font-size: 20px;
  margin-bottom: 2px;
}

/* 通知图标样式 */
.notification-icon-container {
  position: relative;
  display: inline-block;
}

.notification-badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background-color: #f56c6c;
  color: white;
  border-radius: 10px;
  min-width: 16px;
  height: 16px;
  font-size: 10px;
  line-height: 16px;
  text-align: center;
  padding: 0 4px;
}

/* 为底部导航添加安全区域 - 支持 iPhone X 及以上机型 */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .mobile-bottom-nav {
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }
}
</style> 